/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * OpenMapFileFrame.java
 *
 * Created on Mar 30, 2010, 12:04:46 PM
 */

package frame;

import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import objects.Map;
import objects.ScanningLocation;

/**
 *
 * @author Nam
 */
public class OpenMapFileFrame extends javax.swing.JFrame
{
    Map map;
    File inputFile;
    /** Creates new form OpenMapFileFrame */
    public OpenMapFileFrame(File inputMap)
    {
        initComponents();
        this.setResizable(false);
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.map = new Map().readMapFile(inputMap);
        this.inputFile = inputMap;
        //System.out.println(inputMap.getPath());

        //fill the content
        this.lbMapName.setText(map.getMapName());
        this.lbTotalLocations.setText(String.valueOf(map.getLocationList().size()));
        this.txtMapContent.setText(map.convertMapToString());
    }
    public OpenMapFileFrame()
    {
        initComponents();
        this.setResizable(false);
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        lbMapName = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtMapContent = new javax.swing.JTextArea();
        jLabel2 = new javax.swing.JLabel();
        lbTotalLocations = new javax.swing.JLabel();
        btnImportLocation = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(localization.LocalizationApp.class).getContext().getResourceMap(OpenMapFileFrame.class);
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, resourceMap.getString("jPanel1.border.title"), javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), resourceMap.getColor("jPanel1.border.titleColor"))); // NOI18N
        jPanel1.setName("jPanel1"); // NOI18N

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        lbMapName.setText(resourceMap.getString("lbMapName.text")); // NOI18N
        lbMapName.setName("lbMapName"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        txtMapContent.setColumns(20);
        txtMapContent.setRows(5);
        txtMapContent.setName("txtMapContent"); // NOI18N
        jScrollPane1.setViewportView(txtMapContent);

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        lbTotalLocations.setText(resourceMap.getString("lbTotalLocations.text")); // NOI18N
        lbTotalLocations.setName("lbTotalLocations"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 356, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(6, 6, 6)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(lbTotalLocations))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(lbMapName)))))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(lbMapName))
                .addGap(4, 4, 4)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(lbTotalLocations))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 158, Short.MAX_VALUE)
                .addContainerGap())
        );

        btnImportLocation.setText(resourceMap.getString("btnImportLocation.text")); // NOI18N
        btnImportLocation.setName("btnImportLocation"); // NOI18N
        btnImportLocation.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnImportLocationMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnImportLocation))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnImportLocation)
                .addContainerGap(18, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnImportLocationMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnImportLocationMouseClicked
        // TODO add your handling code here:
        JFileChooser fileChooser = new JFileChooser();
        int returnVal = fileChooser.showOpenDialog(getContentPane());
        if(returnVal==JFileChooser.APPROVE_OPTION)
        {
            try
            {
                File locationFile = fileChooser.getSelectedFile();
                ScanningLocation m_location = new ScanningLocation().readScanningResultFromFile(locationFile);
                m_location.printResult();
                map.addLocation(m_location);
                map.createMapFile(inputFile);
                this.txtMapContent.setText(map.convertMapToString());
                this.lbTotalLocations.setText(String.valueOf(map.getLocationList().size()));
            }
            catch(Exception ex)
            {
                ex.printStackTrace();
            }
        }
    }//GEN-LAST:event_btnImportLocationMouseClicked

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new OpenMapFileFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnImportLocation;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lbMapName;
    private javax.swing.JLabel lbTotalLocations;
    private javax.swing.JTextArea txtMapContent;
    // End of variables declaration//GEN-END:variables

}
